package com.hxx.day8;

import org.junit.Test;

import java.io.File;

/**
 * File
 */
public class TestDemo3 {

    @Test
    public void test1()throws Exception{
        File file = new File("d:/zmx/aa.txt");
        File file1 = new File("d:/zmx", "aa.txt");
        System.out.println(file.exists());
        System.out.println(file1.exists());
    }
    /**
     * 递归遍历目录
     */
    @Test
    public void test2()throws Exception{
        File file = new File("d:/zmx");
        printFile(file);
    }
    public void printFile(File file){
        File[] files = file.listFiles();
        for (File f : files) {
            if(f.isDirectory()){
                printFile(f);
            }else {
                System.out.println("文件名称:"+f);
            }
        }
    }
    /**
     * 递归删除目录
     */
    @Test
    public void test3()throws Exception{
        File file = new File("d:/zmx");
        deleteDir(file);
    }
    public void deleteDir(File file){
        if(file.isDirectory()){
            File[] files = file.listFiles();
            for (File f : files) {
                if(f.isDirectory()){
                    deleteDir(f);
                }else {
                    f.delete();
                }
            }
        }
        file.delete();//删除空目录，最后一级目录
    }
}


























